
import os

def get_time(a_post):
    separated=list(a_post.split('\t'))
    separated[-1]=separated[-1][:-1]
    eles=separated[-1].split(' ')
    month_lst=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec']
    month=str(month_lst.index(eles[1])+1)
    if (len(month)==1):
        month='0'+month
    year=eles[-1]
    day=eles[2]
    return year+month+day

def sort_document(summary_file_path):
    print("文档分类中...")
    count=0
    with open(summary_file_path,
              'r',
              encoding='utf-8') as file:
        for line in file:
            file_time=get_time(line)
            path_of_file='C:/Users/Administrator/Desktop/using_python/sort_document_files/'+file_time+'.txt'
            print(path_of_file)
            if not (os.path.exists(path_of_file)):
                f=open(path_of_file,'w')
                f.close()
            temp_file=open(path_of_file,'a',encoding='gb18030',errors='ignore')
            temp_file.write(line)
            temp_file.close()
            count+=1
            print("当前进度%.2f%%"%(1.0*count/1000))
    print("分类结束。")


